﻿Imports System.Net
Imports System.Text
Imports System.Xml
Imports System.IO
Imports System.Data

Partial Public Class fbDS2
    Partial Class InvoiceDataTable
        Sub Fill(ByVal dataSet As DataSet)
            'fbDS.Merge(fbGetTable("invoice.list", "invoice"))
        End Sub
    End Class

    Public Shared Function fbGetTable(ByVal fbMethod As String, ByVal fbTable As String) As DataTable
        Cursor.Current = Cursors.WaitCursor
        Dim fbUrl As String = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\FreshMobile\", "fbUrl", "freshbooksmobile.freshbooks.com")
        Dim fbToken As String = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\FreshMobile\", "fbToken", "123")
        Dim response As HttpWebResponse
        Dim request As HttpWebRequest

        Dim fbApiUrl As String = "https://" & fbUrl & "/api/2.1/xml-in"
        Dim postData As String = "<?xml version=""1.0"" encoding=""utf-8""?><request method=""" & fbMethod & """><per_page>50</per_page></request>"
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)

        ' Set Credentials Cache
        Dim myCred As New NetworkCredential(fbToken, "foo", "")
        Dim credCache As New CredentialCache()
        credCache.Add(New Uri("https://" & fbUrl), "Basic", myCred)

        request = DirectCast(WebRequest.Create(fbApiUrl), HttpWebRequest)
        request.Credentials = credCache
        request.UserAgent = "FreshMobile 1.0"
        request.Method = "POST"
        request.ContentType = "application/xml"
        request.ContentLength = byteArray.Length
        request.AllowWriteStreamBuffering = True
        request.PreAuthenticate = True
        request.Timeout = 10000
        request.KeepAlive = True
        request.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy()

        Try

            'GetStream and Encode PostData
            Dim dataStream As Stream = request.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()


            ' Set Response
            response = DirectCast(request.GetResponse(), HttpWebResponse)
            Dim receiveStream As Stream = response.GetResponseStream()
            Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)

            'Create new dataset and load xml from stream
            Dim ds As New DataSet
            ds.ReadXml(readStream)

            If Not ds.Tables("response").Rows(0)("status") = "ok" Then Throw New Exception("Freshbooks API Update Error:" & vbCrLf & ds.Tables("response").Rows(0)("error"))

            'close connections
            receiveStream.Close()
            readStream.Close()
            response.Close()

            Return ds.Tables(fbTable)

        Catch ex As Exception
            Cursor.Current = Cursors.Default
            MsgBox(ErrorToString, MsgBoxStyle.Exclamation, "Data Request Error")
            request = Nothing
            Return Nothing
        End Try
    End Function
End Class


Partial Public Class fbXml
End Class
